﻿using Lidgren.Network;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(BoxCollider))]
public class TankActor : BaseActor {

    public TankData tankData;
    public GameObject Turret;
    public GameObject ShootingPos;
    public GameObject BulletPrefab;

    protected bool isCanShot
    {
        get
        {
            if (AttackTimer + tankData.attackSpeed < Time.time)
                return true;
            return false;
        }
    }
    protected float AttackTimer;
    Rigidbody _rigidbody;
    BoxCollider boxCollider;
    /// <summary>
    /// Awake is called when the script instance is being loaded.
    /// </summary>
    protected virtual void Awake()
    {
        _rigidbody = this.GetComponent<Rigidbody>();
        boxCollider = this.GetComponent<BoxCollider>();
        
     
    }

    public override void InitStart()
    {
        base.InitStart();

        AttackTimer = Time.time;
        Log.Info(this.ActorInfo.name + "InitStart  " + isLocalPlayer);
        if (isLocalPlayer)
        {
        
            InputHandler.Instantce.OnKeyMove += Move;
        }
            
    }
    /// <summary>
    /// This function is called when the MonoBehaviour will be destroyed.
    /// </summary>
    protected virtual void OnDestroy()
    {
        //if(isLocalPlayer)
        //    InputHandler.Instantce.OnKeyMove-=Move;
    }
    /// <summary>
    /// Update is called every frame, if the MonoBehaviour is enabled.
    /// </summary>
    protected virtual void Update()
    {
        

        PlayerControl();
    }

    protected virtual void PlayerControl()
    {
        if (!isLocalPlayer)
            return;
        if (Input.GetKeyUp(KeyCode.J))
        {
            Shooting();
        }
    }

    public virtual void Move(Vector2 dir)
    {
        if(dir == Vector2.zero)
        return;
        var v3Dir =  new Vector3(dir.x,0,dir.y);

        var angle = Vector3.Angle(this.transform.forward,v3Dir);
         
        var LookRotation= Quaternion.LookRotation(v3Dir,this.transform.up);
        //this.transform.rotation = Quaternion.Lerp(this.transform.rotation,LookRotation,Time.deltaTime * tankData.turnSpeed);
        this.transform.rotation = LookRotation;
         if (angle >= 100)
         {
          return;   
         }
        //if(angle <= 45)
        //{
        _rigidbody.MovePosition(_rigidbody.position + this.transform.forward * tankData.moveSpeed * Time.deltaTime);
            //this.transform.position += this.transform.forward* tankData.moveSpeed * Time.deltaTime;
        //}
       
    }

    public virtual void TurretController(Vector2 dir)
    {
        if (dir == Vector2.zero)
            return;
        var v3Dir = new Vector3(dir.x, 0, dir.y);

        var angle = Vector3.Angle(this.transform.forward, v3Dir);

        var LookRotation = Quaternion.LookRotation(v3Dir, this.transform.up);
        Turret.transform.rotation = Quaternion.Lerp(Turret.transform.rotation, LookRotation, Time.deltaTime * tankData.turnSpeed);
    }

    public virtual void Shooting()
    {
        if (!isCanShot)
            return;
        var bulletGo = Instantiate(BulletPrefab, ShootingPos.transform);
        BulletPrefab.transform.localPosition = Vector3.zero;
        bulletGo.GetComponent<Rigidbody>().AddForce(bulletGo.transform.forward * 1000);
        bulletGo.transform.SetParent(null);

        AttackTimer = Time.time;
    }



}
